/**
 *
 * © Bahadir Zeren 2011
 * bzeren@gmail.com
 * No warranties, express or implied, are made for this program.
 *
 */

package org.baatar.opt.genetic.impl.prmopt;

import java.util.ArrayList;
import java.util.BitSet;
import org.baatar.opt.genetic.GeneticIterationListener;
import org.baatar.opt.genetic.GeneticOptimizer;
import org.baatar.opt.genetic.chromosome.IChromosome;
import org.baatar.opt.genetic.crossover.UniformCrossover;
import org.baatar.opt.genetic.selection.BinaryTournamentSelection;

/**
 *
 * @author Ba
 */
public class main {
    public static void main(String args[]) throws Exception {

        ArrayList<ParamInfo> paramInfos = new ArrayList<ParamInfo>();

        paramInfos.add(new ParamInfo("x", -1000, 1000, 16));
        paramInfos.add(new ParamInfo("y", -1000, 1000, 16));

        ChromosomeFactory chromosomeFactory = new ChromosomeFactory(paramInfos,
                                                                    "return (x-50)*(x-50)+(y-80)*(y-80);");

        GeneticOptimizer<Boolean, BitSet> go = new GeneticOptimizer<Boolean, BitSet>();

        go.setMaxNumOfIterations(500);
        go.setMaxNumOfIterationsWOProgress(200);
        go.setPopulationSize(100);
        go.setMinNumOfChildren(20);
        go.setNumOfEliteChromosomes(4);
        go.setMutationRate(0.01f);
        go.setGreedRate(0.0f);
        go.setAllowDublicateChromosomes(false);

        go.setChromosomeFactory(chromosomeFactory);
        go.setSelector(new BinaryTournamentSelection<Boolean, BitSet>());
        go.setCrossoverOperator(new UniformCrossover<Boolean, BitSet>());
        go.setMutator(new Mutator());

        go.setGeneticIterationListener(new GeneticIterationListener<Boolean, BitSet>() {
            @Override
            public void onProgress(int optimizerIndex, int iteration, double elapsedTime, String info) {
                System.out.println(iteration + " - " + elapsedTime + " sn; " + info);
            }

//            @Override
//            public void onIterate(int optimizerIndex, int iteration, int numOfIterationsPassedWOProgress, int maxNumOfIterations) {
//            }

            @Override
            public void onMigrationNeeded(int optimizerIndex, int iteration, IChromosome<Boolean, BitSet> migrants[]) {
            }

            @Override
            public void onException(int optimizerIndex, String exceptionMessage) {
            }
        });

        try {
            if (go.initialize())
                go.start();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
}
